<?php
namespace app\home\controller;
use think\Db;
class News extends HomeBase {

	public function getlist() {
		$map = array();
		$catid = input('catid');
		$uid = input('uid');
		$order = input('order');	// 排序
		$page = input('page','1','intval');		// 当前页
		$row = input('row','10','intval');		// 每页显示数据

		// 查询条件
		if ($catid = intval($catid)) $map['catid'] = $catid;
		// if ($uid = intval($uid)) $map['uid'] = $uid;

		// 排序
		if (empty($order)) $order = 'sort asc, create_time desc';
		
		// 分页
		$start = ($page-1) * $row;

		if (!empty($map)) {
			$total = Db::name('News')->where($map)->count('id');
			$list = Db::name('News')->where($map)->order($order)->limit($start,$row)->field('id,title,content,catid,create_time,update_time')->select();
		} else {
			$total = Db::name('News')->count('id');
			$list = Db::name('News')->order($order)->limit($start,$row)->field('id,title,content,catid,create_time,update_time')->select();
		}
		if (empty($list)) 
			return json(array('status'=>0,'msg'=>'暂无数据'));
		else {
			foreach ($list as &$value) {
				$value['content'] = $this->getDescriptionFromContent($value['content']);
				$cat = Db::name('Cat')->where(array('id'=>$value['catid'],'mark'=>'news'))->field('title')->find();
				$value['cat'] = $cat['title'];
				$value['create_time'] = date('Y.m.d',$value['create_time']);
				$value['update_time'] = date('Y.m.d',$value['update_time']);
			}
			return json(array('status'=>1,'info'=>$list,'count'=>$total));
		}
	}

	public function getdetail() {
		$newid = input('newid');
		$newid = intval($newid);
		$info = Db::name('News')->where('id',$newid)->find();
		if (!$info)
			return json(array('status'=>0,'msg'=>'数据未找到'));

		$cat = Db::name('Cat')->where(array('id'=>$info['catid'],'mark'=>'news'))->field('title')->find();
		$info['cat'] = $cat['title'];
		// 刷新浏览次数
		Db::name('News')->where('id',$newid)->setInc('nums');
		return json(array('status'=>1,'info'=>$info));

	}
}